package com.d3.sx_eprotection.controller;

import com.d3.sx_eprotection.entity.*;
import com.d3.sx_eprotection.service.AQIlevelService;
import com.d3.sx_eprotection.service.CityService;
import com.d3.sx_eprotection.service.SupervisorMesgService;
import com.d3.sx_eprotection.service.SupervisorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supervisor")
public class SupervisorController {
    @Autowired
    private SupervisorService supervisorService;

    @Autowired
    private CityService cityService;

    @Autowired
    private SupervisorMesgService supervisorMesgService;
    @Autowired
    private AQIlevelService aqIlevelService;

    @GetMapping("/list")
    public List<Supervisor> list(){
        return supervisorService.list();
    }

    //注册
    @PostMapping("/register")
    public Map<String,Object> register(@RequestBody Map<String,Object> supervisor){
        System.out.println(supervisor);
        Map result = supervisorService.register(supervisor);
        return result;
    }

    //根据省的id查city列表
    @GetMapping("/queryCity")
    public List<City> returnCity(@RequestParam(required = false) Integer id) {
        if (id == null) {
            return new ArrayList<>();
        }
        Province province = new Province();
        province.setId(cityService.find(id).getId());
        return cityService.query(province.getId());
    }
    //公众监督员提交信息
    @PostMapping("/addMesg")
    public int addMesg(@RequestBody SupervisorMesg supervisorMesg){
        return supervisorMesgService.addMesg(supervisorMesg);

    }
    //根据手机号查询监督员历史提交信息
    @GetMapping("/findHistory")
    public List<Map<String,Object>> queryAll(String number){
        List<Map<String,Object>> result = new ArrayList<>();
        List<SupervisorMesg> mesgList = supervisorMesgService.findAll(number);
        for (SupervisorMesg mesg:mesgList){
            Map<String,Object> map = new HashMap<>();
            map.put("address",mesg.getAddress());
            map.put("level",mesg.getLevel());
            map.put("message",mesg.getMessage());
            map.put("supervisor_phone",mesg.getSupervisor_phone());
            map.put("date",mesg.getDate());
            map.put("time",mesg.getTime());
            map.put("grider_id",mesg.getGrider_id());
            map.put("status",mesg.getStatus());
            map.put("id",mesg.getId());
            map.put("province",cityService.find(mesg.getProvince()));
            map.put("city",cityService.findCity(mesg.getCity()));
            result.add(map);
        }
        return result;
    }
    //公众监督员个人信息获取
    @GetMapping("/getMsg")
    public Supervisor getMsg(String phoneNumber){
        return supervisorService.getById(phoneNumber);
    }
    //获取等级信息列表
    @GetMapping("/getLevelMsg")
    public List<AQIlevel> getLevel(){
        return aqIlevelService.getList();
    };
    //更新个人信息
    @PostMapping("/updateMesg")
    public  Boolean changeMesg(@RequestBody Supervisor supervisor){
        return supervisorService.updateById(supervisor);
    }
}
